//
//  LMParserProtocol.swift
//  LMNetwork
//
//  Created by zhuo on 2018/7/24.
//

import UIKit

public protocol LMBaseParserProtocol: AnyObject {
    func parserResponseJSONObject(_ json: Any, data: Data?) -> LMNetworkError?
}

open class LMBaseParser: LMBaseParserProtocol {
    
   // josn
    public var jsonObject: Any?
    
    /// list
    public var datas: [Any]?
    
    public init() {
        
    }
    
    /// parser解析json方法
    ///
    /// - Parameters:
    ///   - json: 经过转化的josn数据
    ///   - data: 原始data数据
    /// - Returns: error是否发生错误
    open func parserResponseJSONObject(_ json: Any, data: Data? = nil) -> LMNetworkError? {
        self.jsonObject = json
        var error: LMNetworkError? = nil
        if let dict = json as? [String: AnyObject] {
            var message: String?
            var statusCode = 0
            if let code = dict[LMServerResponse.resultCode] as? Int {
                statusCode = Int(code)
            }
            if let msg = dict[LMServerResponse.resultMessage] as? NSString {
                message = String(msg)
            }
            if statusCode != 0 {
                error = LMNetworkError.serverStatueCode(code: statusCode, message: message)
            }
        }
        
        return error
    }
}
